package com.glepoch.springbootsecuritymybatis.compoment.filter;

import com.glepoch.springbootsecuritymybatis.compoment.requestwrapper.BodyReaderHttpServletRequestWrapper;
import lombok.extern.slf4j.Slf4j;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@Slf4j
@WebFilter(filterName = "bodyReaderFilter", urlPatterns = "/*")
public class BodyReaderFilter implements Filter {

    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        log.info("进入过滤器，替换request对象");
        ServletRequest requestWrapper = null;
        if (servletRequest instanceof HttpServletRequest){
            requestWrapper = new BodyReaderHttpServletRequestWrapper((HttpServletRequest)servletRequest);
        }
        if (null == requestWrapper){
            filterChain.doFilter(servletRequest,servletResponse);
        }else {
            filterChain.doFilter(requestWrapper,servletResponse);
        }
    }
}
